//
//  MenuButton.m
//  AppleCilent
//
//  Created by Nail Sharipov on 04/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "MenuButton.h"
//#import <QuartzCore/QuartzCore.h>

@implementation MenuButton
{
    int itemId;
    ClickHandler clickHandler;
    UIButton* button;
    UILabel* title;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        float width = frame.size.width;
        float height = frame.size.height;
        button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, width)];
        [button setTitle: @"" forState: UIControlStateNormal];
        [button setBackgroundColor:[UIColor clearColor]];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        CALayer* layer = [button layer];
        [layer setCornerRadius:10.0f];
        [layer setMasksToBounds:YES];
        [layer setBorderColor:[UIColor darkGrayColor].CGColor];
        [layer setBorderWidth:1.0f];
        layer.shouldRasterize = YES;
        
        [self addSubview:button];
        
        float margin = 6.0f;
        float titleHeight = height - width - 6.0f;
        
        title = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, width + margin, width, titleHeight)];
        [title setTextColor: [UIColor darkGrayColor]];
        [title setBackgroundColor:[UIColor clearColor]];
        [title setAdjustsFontSizeToFitWidth:NO];
        [title setAdjustsLetterSpacingToFitWidth:NO];
        [title setTextAlignment:NSTextAlignmentCenter];
        [title setFont: [UIFont boldSystemFontOfSize: 10.0f]];
        [self addSubview:title];

    }
    return self;
}

- (void) setText: (NSString*)text
{
    [title setText: text];
}
- (void) setImage: (UIImage*)image
{
    [button setImage:image forState:UIControlStateNormal];
}

- (void) addClickHandlerEvent: (int)mItemId onClick: (ClickHandler) onClickHandler
{
    itemId = mItemId;
    clickHandler = onClickHandler;
}

-(void)buttonPressed:(id)sender
{
    clickHandler(itemId);
}


@end
